package org.example.util.collection;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Map相关工具类
 *
 * @author Maple
 * @date 2023-07-26
 */
public class MapUtil {

    public static Map<String, ?> toMap (Object object) {
        return (Map<String, ?>) JSON.toJSON(object);
    }

    public static <K, V> Map<K, V> getMap() {
        return new HashMap<>();
    }

    public static <K, V> Map<K, V> getMap(int size) {
        return new HashMap<>(size);
    }

    /**
     * 用于小map，size小于16的
     *
     * @param size 预期size大小，大概值就行
     */
    public static <K, V> Map<K, V> getSmallMap(int size) {
        return size < 16 ? new HashMap<>(size, 2) : getMap(size);
    }

    public static <K, V> Map<K, V> getMap(K k, V v) {
        Map<K, V> mapInstance = getSmallMap(1);
        mapInstance.put(k, v);
        return mapInstance;
    }

    public static JSONObject getJsonMap() {
        return new JSONObject();
    }

    public static JSONObject getJsonMap (String key, Object val) {
        JSONObject jsonObject = getJsonMap();
        jsonObject.put(key, val);
        return jsonObject;
    }

}
